#include "backendtest.h"
#include "filereader.h"
#include <QLayout>
#include <phonon/seekslider.h>
#include <QPushButton>

#if defined(TESTING)
BackendTest::BackendTest(QHostAddress address, int port)
{
    fileReader=new FileReader(address,port);
    connect(fileReader,SIGNAL(completed(QByteArray*)),this,SLOT(nextStep(QByteArray *)));
    in=new QHttp;
    buffer=new QBuffer;
    buffer->open(QIODevice::ReadWrite);
    connect(in,SIGNAL(requestFinished(int,bool)),this,SLOT(test()));
    in->get("http://gloriaandal.com/sites/default/files/video2.m4v",buffer);
    //qDebug()<<"result is "<<QString(res->buffer());

}

void BackendTest::nextStep(QByteArray * byteArray) {
    QString result=QString(*byteArray);
    qDebug()<<"result retrieved"<<result;
    QStringList list=result.split(QRegExp("( |\n)"));
    QList<QStringList> list2;


    qDebug()<<list;
    if(list.at(0)=="Video"&&list.at(1)=="Listing") {
        qDebug()<<"requesting video";
        QStringList byLine= result.split(QRegExp("(\n)"));
        foreach(QString line,byLine) {
            list2.append(line.split(QRegExp("(; )")));
        }
        qDebug()<<list2;

        connect(fileReader,SIGNAL(downloadComplete(QByteArray*)),this,SLOT(playVideo(QByteArray*)));
        fileReader->requestFile(QVariant(list2.at(1).at(0)).toInt());
    } else if(list.at(0)=="Photo"&&list.at(1)=="Listing") {
        connect(fileReader,SIGNAL(downloadComplete(QByteArray*)),this,SLOT(showImage(QByteArray*)));
        QStringList byLine= result.split(QRegExp("(\n)"));
        foreach(QString line,byLine) {
            list2.append(line.split(QRegExp("(; )")));
        }
        fileReader->requestFile(QVariant(list2.at(1).at(0)).toInt());
    }
}



void BackendTest::start() {
    //fileReader->getListing("Video");
    QWidget * parent=new QWidget();
    player=new Phonon::VideoPlayer();
    player->show();
    player->play(QUrl("http://localhost:7008/42"));//*/

    //player->play(QUrl("http://www.gloriaandal.com/site/sites/default/files/video2.m4v"));
    /*Phonon::MediaSource * source=new Phonon::MediaSource("http://localhost:7008/41");

    //
    qDebug()<<"starting stream attempt";
    Phonon::MediaObject * mediaObject=new Phonon::MediaObject();
    mediaObject->setCurrentSource(*source);
    Phonon::VideoWidget * player=new Phonon::VideoWidget();
    player->show();
    Phonon::createPath(mediaObject, player);
    Phonon::SeekSlider * slider=new Phonon::SeekSlider(mediaObject);
    player->setLayout(new QVBoxLayout());
    QHBoxLayout * bottom=new QHBoxLayout();
    parent->setLayout(bottom);
    bottom->addWidget(slider);
    QPushButton * playButton =new QPushButton("Play",parent);
    bottom->addWidget(playButton);
    connect(playButton,SIGNAL(clicked()),mediaObject,SLOT(play()));
    player->layout()->addWidget(parent);
    Phonon::AudioOutput *audioOutput =
             new Phonon::AudioOutput(Phonon::MusicCategory, this);
         Phonon::Path path = Phonon::createPath(mediaObject, audioOutput);
         audioOutput->setVolume(1);
    qDebug()<<"let's dance";//*/
}

void BackendTest::playVideo(QByteArray *video) {
    disconnect(fileReader,SIGNAL(downloadComplete(QByteArray*)),this,SLOT(playVideo(QByteArray*)));
    player=new Phonon::VideoPlayer();
    player->show();
    player->play(Phonon::MediaSource(new QBuffer(video)));
    fileReader->getListing("Photo");

}

void BackendTest::showImage(QByteArray *imageData) {
    disconnect(fileReader,SIGNAL(downloadComplete(QByteArray*)),this,SLOT(showImage(QByteArray*)));
    QImage image=QImage::fromData(*imageData);
    widget=new QWidget();
    label=new QLabel();
    label->setPixmap(QPixmap::fromImage(image));
    label->show();
}

void BackendTest::writeHead(QHttpResponseHeader header) {
    qDebug()<<"header is"<<header.toString();
}

void BackendTest::test() {
    qDebug()<<"header is"<<QString(buffer->buffer());
}



#endif
